package com.music.muisc.controllers;

import com.music.muisc.dao.QuizRepository;
import com.music.muisc.entity.Quiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("quizzes")
public class QuizController {

    @Autowired
    QuizRepository quizRepository;
    @GetMapping
    public String displayQuizzes(Model model){
        List<Quiz> quizzes = (List<Quiz>) quizRepository.findAll();
        model.addAttribute("quizzes", quizzes);

        return "quizzes/list-quizzes";
    }
    @GetMapping("/new")
    public String displayQuizForm(Model model){
        Quiz aQuiz = new Quiz();
        model.addAttribute("quiz", aQuiz);
        return "quizzes/new-quiz";
    }
    @PostMapping("/save")
    public String createQuiz(Model model, Quiz quiz) {

        //处理保存数据库行为
        quizRepository.save(quiz);
        return "redirect:/quizzes/new";
    }


}
